﻿// ------------------------------------------------------------------------------------------------
//  <copyright file="Main.cs" company="Iveely">
//    Copyright (c) Iveely Liu.  All rights reserved.
//  </copyright>
//  
//  <Create Time>
//    12/03/2012 20:12 
//  </Create Time>
//  
//  <contact owner>
//    liufanping@iveely.com 
//  </contact owner>
//  -----------------------------------------------------------------------------------------------

#region

using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Windows.Forms;
using IveelySE.Common;
using IveelySE.NLP.Index;
using IveelySE.Tools.Common;
using System.IO;
using IveelySE.NLP.Common.DataStructure;
using System.Text;

#endregion

namespace IveelySE.Tools
{
    public partial class Main : Form
    {
        public Main()
        {
            InitializeComponent();
        }

        private void pageToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string file = DialogUtil.OpenFile("Iveely Page Files (*.PAGE)|*.txt|All files (*.*)|*.*");
            if (file != "")
            {
                var pageList = (List<PObject>)Serialize.UnSerailzable(file);
                this.dataGrid.DataSource = (from pObject in pageList select pObject.Value).ToList();
            }
            this.dataGrid.Refresh();
        }

        private void indexToolStripMenuItem_Click(object sender, EventArgs e)
        {
           
        }

        private void pageRankToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string file = DialogUtil.OpenFile("Iveely Rank Files (*.PageRank)|*.txt|All files (*.*)|*.*");
            if (file != "")
            {
                var ranks = (Hashtable)Serialize.UnSerailzable(file);
                var dt = new DataTable();
                dt.Columns.Add("Url", typeof(string));
                dt.Columns.Add("Rank", typeof(string));
                IDictionaryEnumerator enumerator = ranks.GetEnumerator();
                DataRow row = null;
                while (enumerator.MoveNext())
                {
                    var index = (string)enumerator.Key;
                    string value = enumerator.Value.ToString();
                    row = dt.NewRow();
                    row["Url"] = index;
                    row["Rank"] = value;
                    dt.Rows.Add(row);
                }
                this.dataGrid.DataSource = dt;
            }
        }

        private void Main_Load(object sender, EventArgs e)
        {
        }

        private void knownledgeKToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (File.Exists(Config.Application.GetEventFile()))
            {
                StringBuilder sb = new StringBuilder();
                Hashtable drafts = (Hashtable)Serialize.UnSerailzable(Config.Application.GetEventFile());
                foreach (DictionaryEntry dictionaryEntry in drafts)
                {
                    sb.AppendLine(dictionaryEntry.Key.ToString());
                }
                File.WriteAllText("drafts.txt", sb.ToString());


            }
        }

        private void knownledgeIndexKLToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Invert index = (Invert)Serialize.UnSerailzable(Config.Application.GetEventIndex());
        }

        private void localSearchToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SearchMode searchMode = new SearchMode();
            searchMode.ShowDialog();
        }
    }
}